package com.jackchong.utils;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.PopupWindow;

import androidx.annotation.NonNull;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.LifecycleRegistry;

/**
 * author : chong.huang
* create : 2/13/21
 * desc   :
 */
public class JPopupWindow extends PopupWindow implements LifecycleOwner {

    private final LifecycleRegistry mLifecycleRegistry = new LifecycleRegistry(this);

    public JPopupWindow(View contentView) {
        super(contentView);
    }

    public JPopupWindow(int width, int height) {
        super(width, height);
    }

    public JPopupWindow(View contentView, int width, int height) {
        super(contentView, width, height);
    }

    public JPopupWindow(View contentView, int width, int height, boolean focusable) {
        super(contentView, width, height, focusable);
        getLifecycle().setCurrentState(Lifecycle.State.INITIALIZED);
    }

    @Override
    public void showAtLocation(View parent, int gravity, int x, int y) {
        super.showAtLocation(parent, gravity, x, y);
        getLifecycle().setCurrentState(Lifecycle.State.CREATED);
        getLifecycle().setCurrentState(Lifecycle.State.STARTED);
        getLifecycle().setCurrentState(Lifecycle.State.RESUMED);
    }

    @Override
    public void dismiss() {
        super.dismiss();
        getLifecycle().setCurrentState(Lifecycle.State.DESTROYED);
    }

    @NonNull
    @Override
    public LifecycleRegistry getLifecycle() {
        return mLifecycleRegistry;
    }
}
